package com.lucksoft.map.cost.service;

import java.math.BigDecimal;
import java.math.RoundingMode;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.lucksoft.core.response.ApiResult;
import com.lucksoft.core.constant.BaseOperateConstant;
import com.lucksoft.map.cost.param.MapCostParam;

/**
 * 地图费用计算服务类
 * 
 * @author Qwen
 * @date 2025/10/13
 * @version 1.0.0
 */
@Service
public class MapCostService {
    
    private static final Logger logger = LoggerFactory.getLogger(MapCostService.class);
    
    /**
     * 计算费用
     * 
     * @param distance 里程数(公里)
     * @param unitPrice 单价(元/公里)
     * @return ApiResult 费用计算结果
     */
    public ApiResult calculateCost(Double distance, Double unitPrice) {
        try {
            // 参数验证
            if (distance < 0) {
                return new ApiResult(BaseOperateConstant.FAILURE_CODE, "里程数不能为负数", null);
            }
            
            if (unitPrice < 0) {
                return new ApiResult(BaseOperateConstant.FAILURE_CODE, "单价不能为负数", null);
            }
            
            // 计算费用
            BigDecimal distanceBd = BigDecimal.valueOf(distance);
            BigDecimal unitPriceBd = BigDecimal.valueOf(unitPrice);
            BigDecimal cost = distanceBd.multiply(unitPriceBd).setScale(2, RoundingMode.HALF_UP);
            
            // 构建返回结果
            MapCostParam result = new MapCostParam();
            result.setDistance(distance);
            result.setUnitPrice(unitPrice);
            result.setCost(cost);
            
            return new ApiResult(BaseOperateConstant.SUCCESS_CODE, "计算成功", result);
            
        } catch (Exception e) {
            String errorMsg = "费用计算错误: " + e.getMessage();
            logger.error(errorMsg, e);
            return new ApiResult(BaseOperateConstant.FAILURE_CODE, errorMsg, null);
        }
    }
}